home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / basic / BitmapPrint.lha / BitmapPrint / BitmapPrint.asc < prev    next >
Text File  |  2000-08-30  |  5KB  |  189 lines

  1.  
  2. ; $VER: BitmapPrint.bb2 v1.1 (30.08.2000)
  3.  
  4. ; Original authors: David McMinn, Curt Esser
  5. ; Additional modifications: Damir Arh
  6. ; E-mail: damir.arh@telesat.si
  7. ; WWW:    http://damir.gajba.net/
  8.  
  9. ; This source code is completely free. You can redistribute
  10. ; and/or modify it without any restrictions whatsoever.
  11.  
  12. ;-------------------------------------------------------------
  13.  
  14. ; This function is an easy way to print text to any bitmap
  15. ; using standard intuition fonts.
  16.  
  17. ; At the end of this source ('main' label) there's a small
  18. ; demo to show you, what is possible and how you could use
  19. ; this function.
  20.  
  21. ;-------------------------------------------------------------
  22.  
  23.  
  24. ;-------------------------------------------------------------
  25. .BitmapPrint:
  26. ;-------------------------------------------------------------
  27.  
  28. ; This the actual text printing function. Before using it
  29. ; you must have a bitmap opened and an intuifont loaded
  30. ; before calling the function.
  31.  
  32. ; The text will get printed on the current bitmap using
  33. ; the current intuifont. If the text wouldn't fit on the
  34. ; bitmap it won't get printed as this would cause a mess.
  35.  
  36. ; Function parameters:
  37.  
  38. ; text$ = The string to print. You can use Chr$(167) to
  39. ;         separate several lines of text.
  40. ; x     = The x location to center text horizontally on.
  41. ; y     = The y location to print the text.
  42. ; color = The variable contains color number for the
  43. ;         text, outline and shadow. The value is obtained
  44. ;         by calling the BP_GetColor{} function.
  45. ; style = The style of the printing. Add the constants to
  46. ;         get the desired style (some fonts don't support
  47. ;         all styles (it's a limitation of the font):
  48.  
  49. #BP_Normal     = 0
  50. #BP_Bold       = 1
  51. #BP_Underlined = 2
  52. #BP_Italic     = 4
  53. #BP_Extended   = 8
  54. #BP_Shadow     = 16
  55. #BP_Outline    = 32
  56.  
  57. Statement BitmapPrint{text$,x.w,dummy.l,y.w,color.l,style.l}
  58.  
  59.   If Asc(Left$(text$,1))<>0 ; is there anything to print
  60.  
  61.     ; extract shadow information
  62.     shadow = style BitTst 4
  63.     style = style BitClr 4
  64.     shadowcol.w = color MOD 256
  65.     color = color / 256
  66.  
  67.     ; extract outline information
  68.     outline = style BitTst 5
  69.     style = style BitClr 5
  70.     outlinecol.w = color MOD 256
  71.     color = color / 256
  72.  
  73.     enabl.l = 79 ; the enable bits for setting the softstyle
  74.  
  75.     ; create a rastport for the bitmap
  76.     DEFTYPE._RastPort rp
  77.     InitRastPort_ &rp
  78.     rp\_BitMap = Addr BitMap(Used BitMap)
  79.  
  80.     ; set the font and its attributes
  81.     SetFont_ &rp,Peek.l(Addr IntuiFont(Used IntuiFont)+8)
  82.     SetDrMd_ &rp,0 ; print letters only, no backgroud rectangle
  83.     styl.l = SetSoftStyle_(&rp,style,enabl) ; set text style
  84.  
  85.     BMwidth.w=8*rp\_BitMap\BytesPerRow
  86.     BMheight.w=rp\_BitMap\Rows
  87.     FontSize.w=rp\Font\tf_YSize
  88.  
  89.     ; get the number of lines to print
  90.     lines = 1
  91.     For i = 1 To Len(text$)-1
  92.       If Mid$(text$,i,1)=Chr$(167)
  93.         lines = lines + 1
  94.       EndIf
  95.     Next i
  96.  
  97.     j = 1 ; current postion in the text
  98.     y = y - FontSize
  99.     For i = 1 To lines
  100.       s=j ; start of the line
  101.       l=0 ; length of the line
  102.       While j <= Len(text$) AND Mid$(text$,j,1)<>Chr$(167)
  103.         l = l + 1
  104.         j = j + 1
  105.       Wend
  106.       j = j + 1
  107.       y = y + FontSize
  108.  
  109.       If l>0 ; if there is anything to print
  110.         temp$ = Mid$(text$,s,l) ; get the text for this line
  111.         pixels.w=TextLength_(&rp,&temp$,l)
  112.  
  113.         cx=x-pixels/2 ; text centering
  114.  
  115.         ; check if the text fits
  116.         If (cx+2+pixels<BMwidth) AND (cx-1>=0) AND (y-FontSize-1>=0) AND (y+2<BMheight)
  117.           ; print the shadow
  118.           If shadow
  119.             Move_ &rp,cx+2,y+2
  120.             SetAPen_ &rp,shadowcol
  121.             Text_ &rp,&temp$,Len(temp$)
  122.           EndIf
  123.           ; print the outline
  124.           If outline
  125.             SetAPen_ &rp,outlinecol
  126.             Move_ &rp,cx+1,y
  127.             Text_ &rp,&temp$,Len(temp$)
  128.             Move_ &rp,cx,y+1
  129.             Text_ &rp,&temp$,Len(temp$)
  130.             Move_ &rp,cx-1,y
  131.             Text_ &rp,&temp$,Len(temp$)
  132.             Move_ &rp,cx,y-1
  133.             Text_ &rp,&temp$,Len(temp$)
  134.           EndIf
  135.           ;print the text
  136.           Move_ &rp,cx,y
  137.           SetAPen_ &rp,color
  138.           Text_ &rp,&temp$,Len(temp$)
  139.         EndIf
  140.       EndIf
  141.     Next i
  142.   EndIf
  143.  
  144. End Statement
  145.  
  146.  
  147. ;-------------------------------------------------------------
  148. .BP_GetColor:
  149. ;-------------------------------------------------------------
  150.  
  151. ; This a support function for generating a color value
  152. ; needed when calling the BitmapPrint{} function.
  153.  
  154. ; Function parameters:
  155.  
  156. ; text    = Pen color for the text.
  157. ; shadow  = Pen color for the shadow.
  158. ; dummy = A throwaway value to compensate for the
  159. ;         function bug in some versions of AcidLibs.
  160. ; outline = Pen color for the outline.
  161.  
  162. Function.l BP_GetColor{text.q,shadow.q,dummy.l,outline.q}
  163.   Function Return (((text*256)+outline)*256)+shadow
  164. End Function
  165.  
  166.  
  167. ;------------------------------------------------------------
  168. .main:
  169. ;------------------------------------------------------------
  170.  
  171. ; This is just a short program to test the above functions.
  172.  
  173. ; It shows, how to use the functions correctly and shouldn't
  174. ; be directly copied into your program.
  175.  
  176. WBStartup
  177. WBenchToFront_
  178. FindScreen 0
  179. LoadFont 0,"Diamond.font",12
  180. Window 0,10,10,300,150,$1000,"Print Demo",1,0
  181. BitMap 0,300,150,2
  182. Cls 3
  183. BitmapPrint{"Outline, Italic",150,0,15,BP_GetColor{2,0,0,0},#BP_Italic}
  184. BitmapPrint{"Two"+Chr$(167)+"Lines",150,0,40,BP_GetColor{3,1,0,2},#BP_Outline+#BP_Shadow}
  185. BitMaptoWindow 0,0
  186. MouseWait
  187. End
  188.  
  189.